﻿/*
* JQUERY SIMPLE LOGGER - extending JQuery library with logging functionality.* 
* Author       : Makarand Thengdi
* Created      : 23 March 2013 * 
* Depends On   : jQuery.js
*
* Description  : 
* This library is used to log client side events to browser's console window. This is dependent on JQuery.
*
* There are four levels of logging:
*     (1) Error    - to log exceptions.
*     (2) Warning  - to log warnings.
*     (3) Info     - to log high level information.
*     (4) Debug    - to log low level information.
*     
*
*  So for the above given levels of logging, there are four different functions:
*      (1) logError(message)
*      (2) logWarn(message)
*      (3) logInfo(message)
*      (4) logDebug(message)
*
*  How to use these methods in your code?
*  - Its very simple you just have to call these methods as you call trim method of jQuery.
*    Check out the snippet given below:
*
*      $.logError(''); $.logWarn(''); $.logInfo(''); $.logDebug('');
*
*  By default the this logger would show all the levels of logs. But, if you want only specific level to be logged then
*  just update the value of "allowedLoggerLevels".
*
*  Lets say I want to log only errors, then I will write the following line of code before writting any logs.
*       $.allowedLoggerLevels = [$ltype.ERROR];
*        
*  If you want to turn off logging completely, then just wite the following line of code before writting any logs.
*       $.allowedLoggerLevels = [];
*
*  Thats it! yes. This is what you need to get started with simple client side logging.
*/


// Logging Options
// [sample copy] : loggingOptions.TYPE.DEBUG
var loggingOptions = {
    "TYPE": {
        "ERROR"     : "error",
        "WARNING"   : "warning",
        "INFO"      : "info",
        "DEBUG"     : "debug"
    }
}

// alias
var $ltype = loggingOptions.TYPE;

// extending jQuery with mx logger
$.extend({

    allowedLoggerLevels: [$ltype.ERROR, $ltype.WARNING, $ltype.INFO, $ltype.DEBUG],

    // log functions
    logError:   function (message) { this._logEvent($ltype.ERROR, message);    }, // log error    
    logWarn:    function (message) { this._logEvent($ltype.WARNING, message);  }, // log warn    
    logInfo:    function (message) { this._logEvent($ltype.INFO, message);     }, // log info    
    logDebug:   function (message) { this._logEvent($ltype.DEBUG, message);    }, // log debug
    
    // log event
    _logEvent: function (eventType, eventMessage) {

        if (this._isLoggingAllowed(eventType)) {
            
            // formatting the event message
            eventMessage = (this._event_GetType(eventType) + this._event_GetTime() + eventMessage);

            if (window && window.console) {
                try {
                    try {
                        if (eventType == $ltype.ERROR)      { window.console.error(eventMessage);   }
                        if (eventType == $ltype.WARNING)    { window.console.warn(eventMessage);    }
                        if (eventType == $ltype.INFO)       { window.console.log(eventMessage);     }
                        if (eventType == $ltype.DEBUG)      { window.console.debug(eventMessage);   }
                    }
                    catch (ex) { window.console.log(eventMessage); }
                } catch (ex) { /* Do nothing */ }
            }
        }
    },

    // checks whether writing event type is allowed or not
    _isLoggingAllowed: function (eventType) {
        var allowed = false;
        allowed = (typeof this.allowedLoggerLevels != 'undefined' && this.allowedLoggerLevels != null && this.allowedLoggerLevels.length > 0 && $.inArray(eventType, this.allowedLoggerLevels) > -1);
        return allowed;
    },

    // gets current event type string to insert in logs
    _event_GetType: function (eventType) {
        var currEventType;
        switch (eventType) {
            case $ltype.ERROR:      currEventType = '[ERROR] '      ; break;
            case $ltype.WARNING:    currEventType = '[WARNING] '    ; break;
            case $ltype.INFO:       currEventType = '[INFO] '       ; break;
            case $ltype.DEBUG:      currEventType = '[DEBUG] '      ; break;
            default:                currEventType = ''              ; break;
        }
        return currEventType;
    },

    // gets current time to insert in logs
    _event_GetTime: function () {
        var currDate = new Date();
        var currTime = '[' + currDate.getHours() + ':' + currDate.getMinutes() + ':' + currDate.getSeconds() + '] ';
        return currTime;
    }
});